package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.DmcWatchData;
import com.csun.cmny.provider.model.dto.OldmanRpcDto;
import com.csun.cmny.provider.model.dto.watch.WatchHeartQueryDto;
import com.csun.cmny.provider.model.dto.watch.WatchLocationQueryDto;
import com.csun.cmny.provider.model.dto.watch.WatchSleepQueryDto;
import com.csun.cmny.provider.model.dto.watch.WatchStepQueryDto;
import com.csun.cmny.provider.model.vo.OldmanBedRoomBuildingVo;
import com.csun.cmny.provider.model.vo.watch.*;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-01-17 16:11
 * @Description: the class dmc watch data controller
 */
@RestController
@RequestMapping(value = "/watch", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcWatchDataController extends BaseController {

    @Resource
    private DmcWatchDataService dmcWatchDataService;
    @Resource
    private OmcRpcService omcRpcService;
    @Resource
    private DmcWatchDataStepService dmcWatchDataStepService;
    @Resource
    private DmcWatchDataHeartService dmcWatchDataHeartService;
    @Resource
    private DmcWatchDataSleepService dmcWatchDataSleepService;
    @Resource
    private DmcWatchDataLocationService dmcWatchDataLocationService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;

    /**
     * 查询首页数据
     *
     * @param deviceId
     * @return
     */
    //@LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/showDataByDeviceId/{deviceId}", method = RequestMethod.POST)
    public Wrapper showDataByDeviceId(@PathVariable Long deviceId) {

        DmcWatchDataVo dmcWatchDataVo = dmcWatchDataService.showDataByDeviceId(deviceId);

        OldmanRpcDto oldmanRpcDto = new OldmanRpcDto();
        oldmanRpcDto.setOldmanId(dmcWatchDataVo.getOldmanId());
        //OldmanRpcVo oldmanRpcVo = omcRpcService.getOldmanByOldmanId(oldmanRpcDto);

        OldmanBedRoomBuildingVo obrbv = omcRpcService.queryOldmanBedRoomBuildingByOldmanId(dmcWatchDataVo.getOldmanId());
        /**
         * @date    2020-04-15
         * @author  chisj
         * @desc    修复绑定孝心手环的老人被删除了
         */
        if (PublicUtil.isNotEmpty(obrbv)) {
            dmcWatchDataVo.setOldmanName(obrbv.getOldmanName());
        }

        Map<String, Object> map = Maps.newHashMap();
        map.put("watchData", dmcWatchDataVo);
        // map.put("oldmanData", oldmanRpcVo);

       // return WrapMapper.ok(map);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),map);
    }

    /**
     * query real watch data by device id
     *
     * @param deviceId  device id
     *
     * @return  the wrapper.
     */
    //@LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/queryRealWatchDataByDeviceId/{deviceId}", method = RequestMethod.POST)
    public Wrapper queryRealWatchDataByDeviceId(@PathVariable Long deviceId) {

        DmcWatchData dmcWatchData = dmcWatchDataService.queryRealWatchDataByDeviceId(deviceId);

       // return WrapMapper.ok(dmcWatchData);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),dmcWatchData);
    }

    /**
     * 查询孝心手环实时位置信息
     *
     * @param deviceId  device id
     *
     * @return  the wrapper.
     */
    //@LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/queryRealLocationByDeviceId/{deviceId}", method = RequestMethod.POST)
    public Wrapper queryRealLocationByDeviceId(@PathVariable Long deviceId) {


        WatchLocationVo watchLocationVo = dmcWatchDataLocationService
                .queryRealLocationByDeviceId(deviceId);

       // return WrapMapper.ok(watchLocationVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),watchLocationVo);
    }

    /**
     * 获取孝心手环当前位置
     *
     * @param deviceId  device id
     *
     * @return  the wrapper.
     */
    //@LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/getCurrentLocationByDeviceId/{deviceId}", method = RequestMethod.POST)
    public Wrapper getCurrentLocationByDeviceId(@PathVariable Long deviceId) {

        WatchCurrentLocationVo watchCurrentLocationVo =
                dmcWatchDataService.getCurrentWatchLocationByDeviceId(deviceId);

      //  return WrapMapper.ok(watchCurrentLocationVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),watchCurrentLocationVo);
    }

    /**
     * 查询孝心手环历史定位数据
     *
     * @param watchLocationQueryDto watch location query dto
     *
     * @return  the wrapper
     */
    @Validated
   // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/queryHistLocationListWithPage", method = RequestMethod.POST)
    public Wrapper queryHistLocationListWithPage(@Validated @RequestBody WatchLocationQueryDto watchLocationQueryDto,
                                     BindingResult br) {

        PageInfo pageInfo = dmcWatchDataLocationService.queryHistLocationListWithPage(watchLocationQueryDto);

       // return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),pageInfo);
    }

    /**
     * 查询孝心手环步数 -- 天
     *
     * @param watchStepQueryDto watch step query dto
     *
     * @return  the wrapper.
     */
    @Validated
    //@LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/queryHistStepWithDay", method = RequestMethod.POST)
    public Wrapper queryHistStepWithDay(@Validated @RequestBody WatchStepQueryDto watchStepQueryDto,
                                        BindingResult br) {

        WatchStepDayVo watchStepDayVo = dmcWatchDataStepService.queryHistStepWithDay(watchStepQueryDto);

       // return WrapMapper.ok(watchStepDayVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),watchStepDayVo);
    }

    /**
     * 查询孝心手环步数 -- 周
     *
     * @param watchStepQueryDto watch step query dto
     *
     * @return  the wrapper.
     */
    @Validated
    //@LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/queryHistStepWithWeek", method = RequestMethod.POST)
    public Wrapper queryHistStepWithWeek(@Validated @RequestBody WatchStepQueryDto watchStepQueryDto,
                                         BindingResult br) {

        WatchStepWeekVo watchStepWeekVo = dmcWatchDataStepService.queryHistStepWithWeek(watchStepQueryDto);


       // return WrapMapper.ok(watchStepWeekVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),watchStepWeekVo);
    }

    /**
     * 查询孝心手环步数 -- 月
     *
     * @param watchStepQueryDto watch step query dto
     *
     * @return  the wrapper.
     */
    @Validated
   // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/queryHistStepWithMonth", method = RequestMethod.POST)
    public Wrapper queryHistStepWithMonth(@Validated @RequestBody WatchStepQueryDto watchStepQueryDto,
                                         BindingResult br) {

        WatchStepMonthVo watchStepMonthVo = dmcWatchDataStepService.queryHistStepWithMonth(watchStepQueryDto);


       // return WrapMapper.ok(watchStepMonthVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),watchStepMonthVo);
    }

    /**
     * 查询孝心手环心率 -- 天
     *
     * @param watchHeartQueryDto
     *
     * @return the wrapper.
     */
    @Validated
   // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/queryHistHeartWithDay", method = RequestMethod.POST)
    public Wrapper queryHistHeartWithDay(@Validated @RequestBody WatchHeartQueryDto watchHeartQueryDto,
                                        BindingResult br) {

        WatchHeartDayVo watchHeartDayVo = dmcWatchDataHeartService.queryHistHeartWithDay(watchHeartQueryDto);

       // return WrapMapper.ok(watchHeartDayVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),watchHeartDayVo);
    }

    /**
     * 查询孝心手环睡眠 -- 天
     *
     * @param watchSleepQueryDto watch sleep query dto
     *
     * @return  the wrapper.
     */
    @Validated
   // @LogAnnotation(isSaveResponseData = true, isSaveRequestData = true)
    @RequestMapping(value = "/queryHistSleepWithDay", method = RequestMethod.POST)
    public Wrapper queryHistSleepWithDay(@Validated @RequestBody WatchSleepQueryDto watchSleepQueryDto,
                                         BindingResult br) {

        WatchSleepDayVo watchSleepDayVo = dmcWatchDataSleepService.queryHistSleepWithDay(watchSleepQueryDto);

       // return WrapMapper.ok(watchSleepDayVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,cmnyMessageSource.getMessage("operation.success", null),watchSleepDayVo);
    }

}
